﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using BLL.Configuration;
using BLL.Security;
using Web.Areas.Admin.Infrastructure;
using Web.Areas.Admin.Models.Security;
using Web.Infrastructure.Models;
using Web.Infrastructure.Security;
using AuthenticationStatus = BLL.Models.Administrators.AuthenticationStatus;

namespace Web.Areas.Admin.Controllers
{
    /// <summary>
    /// SecurityController
    /// </summary>
    public partial class SecurityController : AdminController
    {
        private readonly ISecurityService _securityService;

        private readonly IGlobalSettings _globalSettings;

        /// <summary>
        /// Initializes a new instance of the <see cref="SecurityController" /> class.
        /// </summary>
        /// <param name="securityService">The security service.</param>
        /// <param name="globalSettings">The global settings.</param>
        public SecurityController(ISecurityService securityService, IGlobalSettings globalSettings)
        {
            _globalSettings = globalSettings;
            _securityService = securityService;
        }

        public virtual ActionResult LogOn(string returnUrl)
        {
            return BackBoneView(MVC.Admin.Shared.Views.Empty);
        }

        public virtual ActionResult LogOnTemplate()
        {
            return BackBoneView(MVC.Admin.Security.Views.LogOn);
        }

        [HttpPost]
        public virtual ActionResult LogOn(LogOnModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Errors = GetErrors;
                return Json(model);
            }

            AuthenticationStatus status =
                _securityService.AuthenticateAdministrator(model.Login, model.Password);
            switch (status)
            {
                case AuthenticationStatus.NotFound:
                     model.Errors = new List<ErrorModel>
                         {
                                            new ErrorModel
                                                {
                                                    Message = @"Акаунт не найден!"
                                                }
                                        };
                    return Json(model);
                case AuthenticationStatus.PaswordInvalid:
                     model.Errors = new List<ErrorModel>
                         {
                                            new ErrorModel
                                                {
                                                    Message = @"Данные не валидны!"
                                                }
                                        };
                    return Json(model);
            }
            var cookie = new AuthenticationCookie(model.Login, UserTypes.Administrator);
            int timeout = 12;
            cookie.Expires = DateTime.Now.AddHours(timeout);
            cookie.AppendToResponse(Response, _globalSettings.Security.AdminAuthenticationCookieName);
            model.SuccessMessage = "Вы успешно авторизированы!";
            return Json(model);
        }

        public virtual ActionResult LogOff()
        {
            var cookie = Response.Cookies[_globalSettings.Security.AdminAuthenticationCookieName];
            if (null != cookie)
                cookie.Expires = DateTime.Now.AddDays(-1);

            return new EmptyResult();
        }
    }
}